import React, { createContext, useReducer } from 'react';
export const DesignRedux = createContext({});

const reduxData = {
  rightNode: {}, // 右侧元素属性
  currentNode: {}, // 当前选中元素
  schema: [], // 节点树
};

const reducer = (globalState: object, res: object) => {
  return {
    ...globalState,
    ...res,
  };
};

export const DesignProvider = (props: any) => {
  //利用useReducer,将当前reducer中需要处理的方法进行导出，
  // useReducer的第一个参数表示要处理的相关逻辑，第二个参数表示初始值
  const [globalState, dispatch] = useReducer(reducer, reduxData);

  return (
    // 在这里我们使用了useContext进行了状态的共享
    <DesignRedux.Provider value={{ globalState, dispatch }}>
      {props.children}
    </DesignRedux.Provider>
  );
};
